package com.campus.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.activity.entity.Activity;
import com.campus.activity.entity.Registration;
import com.campus.activity.mapper.ActivityMapper;
import com.campus.activity.mapper.RegistrationMapper;
import com.campus.activity.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报名信息服务实现类
 * 
 * @author System
 * @since 2024
 */
@Service
public class RegistrationServiceImpl extends ServiceImpl<RegistrationMapper, Registration> implements RegistrationService {

    @Autowired
    private RegistrationMapper registrationMapper;
    
    @Autowired
    private ActivityMapper activityMapper;

    @Override
    @Transactional
    public boolean registerActivity(Integer studentId, Integer activityId) {
        // 检查是否已报名
        if (isStudentRegistered(studentId, activityId)) {
            return false;
        }
        
        // 检查活动是否存在且未过期
        Activity activity = activityMapper.selectById(activityId);
        if (activity == null || activity.getRegistrationDeadline().isBefore(LocalDateTime.now())) {
            return false;
        }
        
        // 检查是否已满员
        Integer currentCount = getCurrentRegistrationCount(activityId);
        if (currentCount >= activity.getMaxParticipants()) {
            return false;
        }
        
        Registration registration = new Registration();
        registration.setActivityId(activityId);
        registration.setStudentId(studentId);
        registration.setRegistrationTime(LocalDateTime.now());
        registration.setRegistrationStatus("待审核");
        registration.setCreateTime(LocalDateTime.now());
        registration.setUpdateTime(LocalDateTime.now());
        registration.setDeleted(0);
        
        return save(registration);
    }

    @Override
    public boolean cancelRegistration(Integer registrationId) {
        Registration registration = new Registration();
        registration.setRegistrationId(registrationId);
        registration.setDeleted(1);
        registration.setUpdateTime(LocalDateTime.now());
        return updateById(registration);
    }

    @Override
    public boolean cancelRegistrationByStudentAndActivity(Integer studentId, Integer activityId) {
        QueryWrapper<Registration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId)
                   .eq("activity_id", activityId)
                   .eq("deleted", 0);
        
        Registration registration = getOne(queryWrapper);
        if (registration != null) {
            return cancelRegistration(registration.getRegistrationId());
        }
        return false;
    }

    @Override
    public boolean updateRegistrationStatus(Integer registrationId, String status) {
        Registration registration = new Registration();
        registration.setRegistrationId(registrationId);
        registration.setRegistrationStatus(status);
        registration.setUpdateTime(LocalDateTime.now());
        return updateById(registration);
    }

    @Override
    public List<Registration> getRegistrationStatus(Integer studentId) {
        return registrationMapper.selectByStudentId(studentId);
    }

    @Override
    public List<Registration> getRegistrationsByActivityId(Integer activityId) {
        return registrationMapper.selectByActivityId(activityId);
    }

    @Override
    public Map<String, Object> getRegistrationStatistics(Integer activityId) {
        Map<String, Object> statistics = new HashMap<>();
        
        Integer totalCount = registrationMapper.countTotalByActivityId(activityId);
        Integer approvedCount = registrationMapper.countApprovedByActivityId(activityId);
        
        statistics.put("totalRegistrations", totalCount);
        statistics.put("approvedRegistrations", approvedCount);
        statistics.put("pendingRegistrations", totalCount - approvedCount);
        
        return statistics;
    }

    @Override
    public List<Registration> queryRegistrationInfo(Integer activityId, String status) {
        QueryWrapper<Registration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        
        if (activityId != null) {
            queryWrapper.eq("activity_id", activityId);
        }
        
        if (StringUtils.hasText(status)) {
            queryWrapper.eq("registration_status", status);
        }
        
        queryWrapper.orderByDesc("registration_time");
        return list(queryWrapper);
    }

    @Override
    public boolean isStudentRegistered(Integer studentId, Integer activityId) {
        Registration registration = registrationMapper.selectByActivityIdAndStudentId(activityId, studentId);
        return registration != null;
    }

    @Override
    public Integer getCurrentRegistrationCount(Integer activityId) {
        Integer count = registrationMapper.countTotalByActivityId(activityId);
        return count != null ? count : 0;
    }

    @Override
    public Integer getApprovedRegistrationCount(Integer activityId) {
        Integer count = registrationMapper.countApprovedByActivityId(activityId);
        return count != null ? count : 0;
    }
}